﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using System.Data;
using Microsoft.Reporting.WebForms;
using System.Collections;
using System.Text;

namespace Catering.Catering_Page.Kufang
{
    public partial class XiaohaoTypeCrossChar : UserIsNull
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadPage();
                tvClass.Attributes.Add("onclick", "OnTreeNodeChecked()");
            }
        }

        private void loadPage()
        {
            if (Session["User"] != null)
            {
                string userid = Session["PersonnelID"].ToString();
                int companyid = 0;
                int groupid = 0;
                int classid = 0;
                bool isAdmin = false;
                Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin);
                Bind_Data_to_Controls.Bind_Tree_OreaAndCalss(tvClass, companyid);
            }
        }

        private ArrayList GetClassIDs()
        {
            ArrayList alClass = new ArrayList();
            foreach (TreeNode itemRoot in tvClass.Nodes)
            {
                foreach (TreeNode itemChild in itemRoot.ChildNodes)
                {
                    if (itemChild.Checked)
                    {
                        alClass.Add(itemChild.Value);
                    }
                }
            }
            return alClass;
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            ArrayList alClass = GetClassIDs();
            if (alClass == null || alClass.Count <= 0)
            {
                return;
            }
            int[] RAWTID = UCRightSearchTool.GetRaw();
            string[] years = UCRightSearchTool.GetYears();
            if (years == null || years.Count() < 0)
            {
                return;
            }

            Dictionary<string, Dictionary<string, decimal>> xiaohao = new Dictionary<string, Dictionary<string, decimal>>();

            foreach (string tempyear in years)
            {
                Dictionary<string, decimal> list = GetReportModel();//班组列表(x轴列表)
                if (!xiaohao.ContainsKey(tempyear + "年")) xiaohao.Add(tempyear + "年", list);
            }
            DataTable dt = new View_Monitor().GetXiaohaoTypeCrossChar(RAWTID, alClass, years);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string yeardate = dt.Rows[i]["YEAR"].ToString();
                    string className = dt.Rows[i]["CName"].ToString();
                    decimal SumNumber = Convert.ToDecimal(dt.Rows[i]["Jine"]);
                    string tempyear = years.Length > 0 ? yeardate + "年" : "";
                    xiaohao[tempyear][className] = SumNumber;
                }
                dt.Dispose();
            }

            string StrX = "";
            string str = "";
            if (xiaohao != null)
            {
                int n = 0;
                StringBuilder Sresult = new StringBuilder("");
                foreach (string stryear in xiaohao.Keys)
                {
                    Sresult.Append("{");
                    Sresult.Append("name:'" + stryear + "',");
                    Sresult.Append("data: [");
                    string StrData = "";
                    foreach (string key in xiaohao[stryear].Keys)
                    {
                        if (n == 0) StrX += "'" + key + "',";
                        StrData += xiaohao[stryear][key] + ",";
                    }
                    StrData = StrData.Length > 0 ? StrData.Substring(0, StrData.Length - 1) : "";
                    Sresult.Append(StrData + "]");
                    if (n == 0) StrX = StrX.Length > 0 ? StrX.Substring(0, StrX.Length - 1) : "";
                    Sresult.Append("},");
                    str = Sresult.ToString();
                    n++;
                }

            }
            StrX = "[" + StrX + "]";
            str = str.Length > 0 ? str.Substring(0, str.Length - 1) : "";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myScript", "GetDataGroup('" + Title + "'," + StrX + "," + str + ");", true);
        }


        /// <summary>
        /// 获取横坐标值,并初始化
        /// </summary>
        /// <param name="groupid"></param>
        /// <param name="classid"></param>
        /// <returns></returns>
        public Dictionary<string, decimal> GetReportModel()
        {
            Dictionary<string, decimal> result = new Dictionary<string, decimal>();

            foreach (TreeNode itemRoot in tvClass.Nodes)
            {
                foreach (TreeNode itemChild in itemRoot.ChildNodes)
                {
                    if (itemChild.Checked)
                    {
                        if (itemChild.Target == "bz")
                        {
                            if (!result.ContainsKey(itemChild.Text)) result.Add(itemChild.Text, 0);
                        }
                    }
                }
            }
            return result;
        }

    }
}